package cn.school.thoughtworks.section2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PracticeC {
    Map<String, Integer> countSameElements(List<String> collection1) {
        Map<String,Integer> map = new HashMap<>();

        for (int i = 0; i < collection1.size(); i++) {
            if(collection1.get(i).length()>1){

                String s = collection1.get(i);
                int index = s.indexOf("-");
                if(index != -1){
                    String[] split = s.split("-");
                    if(map.containsKey(split[0])){
                        int i1 = Integer.valueOf(split[1]) + map.get(split[0]);
                        map.put(split[0],i1);
                    }else{
                        map.put(split[0],Integer.valueOf(split[1]));
                    }
                }

                int index1 = s.indexOf(":");
                if(index1 != -1){
                    String[] split = s.split(":");
                    if(map.containsKey(split[0])){
                        int i1 = Integer.valueOf(split[1]) + map.get(split[0]);
                        map.put(split[0],i1);
                    }else{
                        map.put(split[0],Integer.valueOf(split[1]));
                    }
                }
                int index2 = s.indexOf("]");
                if(index2 != -1){
                    String key = s.charAt(0)+"";
                    String value = s.substring(2,s.length()-1);
                    if(map.containsKey(key)){
                        int i1 = Integer.valueOf(value) + map.get(key);
                        map.put(key,i1);
                    }else{
                        map.put(key,Integer.valueOf(value));
                    }
                }

            }else{
                if(map.containsKey(collection1.get(i))){
                    Integer integer = map.get(collection1.get(i));
                    integer+=1;
                    map.put(collection1.get(i),integer);
                }else {
                    map.put(collection1.get(i),1);
                }
            }

        }

        return map;
    }
}
